﻿using Newtonsoft.Json;
using SmsApiTest.model.Request;
using SmsApiTest.model.Response;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;

namespace SmsApiTest.core
{
    public static class common
    {
        public static string smsApiUrl { get; set; }
        public static string smsAccount { get; set; }
        public static string smsPwd { get; set; }
        public static int smsSaleTemplateId { get; set; }
        public static int smsPurchaseTemplateId { get; set; }

        #region 发送短信

        /// <summary>
        /// 发送模板短信
        /// </summary>
        /// <param name="apiurl"></param>
        /// <returns>返回空成功，其他失败</returns>
        public static string Template(TemplateSmsRequest request)
        {
            var result = "";

            request.Account = smsAccount;
            request.Pwd = smsPwd;

            StringBuilder arge = new StringBuilder();
            arge.AppendFormat("Account={0}", request.Account);
            arge.AppendFormat("&Pwd={0}", request.Pwd);
            arge.AppendFormat("&Content={0}", request.Content);
            arge.AppendFormat("&Mobile={0}", request.Mobile);
            arge.AppendFormat("&SignId={0}", request.SignId);
            arge.AppendFormat("&TemplateId={0}", request.TemplateId);
            //arge.AppendFormat("&SendTime={0}", request.SendTime);
            string resp = common.PushToWeb(smsApiUrl, arge.ToString(), Encoding.UTF8);
            SendSmsResponse response = JsonConvert.DeserializeObject<SendSmsResponse>(resp);
            if (response.Code == 0)
            {
                //成功
            }
            else
            {
                result = response.Message;
                //失败
            }
            return result;
        }
        #endregion


        /// <summary>
        /// 推送数据 POST方式
        /// </summary>
        /// <param name="weburl">POST到的网址</param>
        /// <param name="data">POST的参数及参数值</param>
        /// <param name="encode">编码方式</param>
        /// <returns></returns>
        public static string PushToWeb(string weburl, string data, Encoding encode)
        {
            try
            {
                byte[] byteArray = encode.GetBytes(data);

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(weburl));
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.ContentLength = byteArray.Length;

                Stream newStream = webRequest.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();

                //接收返回信息：
                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                StreamReader aspx = new StreamReader(response.GetResponseStream(), encode);
                string msg = aspx.ReadToEnd();
                aspx.Close();
                response.Close();
                return msg;
            }
            catch (Exception ex)
            {
                //记录错误日志
                return "";
            }
        }

        #region 获取时间戳
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <param name="serverTime">时间</param>
        /// <param name="isMillisecond">是否精确到毫秒</param>
        /// <returns></returns>
        public static string ToUnixStamp(DateTime serverTime)
        {
            var ts = serverTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
        #endregion

        #region 转换时间戳
        /// <summary>
        /// 转换时间戳
        /// </summary>
        /// <param name="unixStamp">时间戳</param>
        /// <param name="isMillisecond">是否精确到毫秒</param>
        /// <returns></returns>
        public static DateTime FromUnixStamp(long unixStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(unixStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }
        #endregion
    }
}
